package com.kehong.flybird.code;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief 游戏地图（世界）
 * @email 122162005@qq.com
 * @date 2018-07-06
 */
public class World extends JPanel {
    private Bird bird;  // 游戏主角
    private Ground ground;  // 游戏地板
    private Column column1; // 第一根柱头
    private Column column2; // 第二根柱头
    private int score;      // 游戏得分
    private BufferedImage background; // 游戏背景图片
    private BufferedImage initImage;
    private BufferedImage gameOverImage;

    private GameState state; // 游戏状态

    public World() throws IOException {
        // 加载背景图资源
        // E:\workplace\kehongborui\app\src\com\kehong\flybird\code\../resources/bg.png
//        System.out.println(System.getProperty("user.dir") + "\\app\\src\\com\\kehong\\flybird\\resources\\0.png");
        background = ImageIO.read(getClass().getResource("resources/bg.png"));
        initImage = ImageIO.read(getClass().getResource("resources/start.png"));
        gameOverImage = ImageIO.read(getClass().getResource("resources/gameover.png"));

        init();

    }

    public void addListener() {
        // 增加鼠标事件监听
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                /*
                 * 如果游戏状态为初始化，则收到鼠标点击事件后，将状态更改为 游戏开始
                 * 如果游戏状态为开始，则收到鼠标点击事件后，则调用小鸟飞扬的方法
                 * 如果游戏状态为结束，则收到鼠标点击事件后，将状态更改为 初始化
                 */
                if (state == GameState.INIT) {
                    state = GameState.START;
                } else if (state == GameState.START) {
                    bird.flappy();
                } else if (state == GameState.OVER) {
                    state = GameState.INIT;
                    try {
                        init();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });

        requestFocus();
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (state == GameState.INIT) {
                    state = GameState.START;
                } else if (state == GameState.START) {
                    bird.flappy();
                } else if (state == GameState.OVER) {
                    state = GameState.INIT;
                    try {
                        init();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
    }
    /**
     * 初始化游戏中的对象
     *
     * @throws IOException
     */
    public void init() throws IOException {
        // 初始化 管道1
        column1 = new Column(320 + 150);
        // 初始化 管道2
        column2 = new Column(320 + 150 + 180);
        // 实例化地板对象
        ground = new Ground();
        bird = new Bird();
        // 初始化状态
        state = GameState.INIT;
        // 初始化分数
        score = 0;
    }

    @Override
    public void paint(Graphics g) {
        // 画背景图
        g.drawImage(background, 0, 0, null);
        // 绘制管道
        column1.paint(g);
        column2.paint(g);
        // 画地板
        ground.paint(g);
        // 绘制小鸟
        bird.paint(g);

        paintScore(g);

        // 根据当前游戏状态绘制 init 图片或者游戏结束的贴图
        if (state == GameState.INIT) {
            g.drawImage(initImage, 0, 0, null);
        } else if (state == GameState.OVER) {  // 游戏结束，绘制游戏结束的贴图
            g.drawImage(gameOverImage, 0, 0, null);
        }
    }

    /**
     * 绘制分数
     *
     * @param g
     */
    public void paintScore(Graphics g) {
        Font font = new Font(Font.MONOSPACED, Font.BOLD, 30);
        g.setFont(font);
        g.setColor(Color.white);
        g.drawString(score + "", 30, 40);
    }

    /**
     * 让其他的对象动起来
     */
    public void action() {
        addListener();
        while (true) {
            // 游戏开始，调用 游戏中的对象的 step 方法
            if (state == GameState.START) {
                column1.step();
                column2.step();
                bird.step();
                if (bird.pass(column1, column2)) {
                    score++;
                }
            }
            // 如果鸟与地面或者柱头相撞，游戏结束
            if (bird.hit(ground, column1, column2)) {
                // 切换游戏状态
                state = GameState.OVER;
            }

            // 地面动起来
            ground.step();
            // 重绘
            repaint();
            try {
                Thread.sleep(18);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
